using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace csharp.common
{
	public static class Database
	{
		public const string POSTGRESQL = "PostgreSQL";
		public const string MYSQL      = "MySQL";
		public const string SQLITE     = "Sqlite";
		
		/*atributes*/
		public enum Status { SUCCESSFUL, FAILED };
		public struct Result
		{
			public Status Status;
			public string ErrorMessage;
		}
		public  static Result result = new Result();
		private static string dbName       = string.Empty;
		private static string dbHost       = string.Empty;
		private static string dbPort       = string.Empty;
		private static string dbUser       = string.Empty;
		private static string dbPassword   = string.Empty;
		private static Boolean dbConnected = false;
		private static string dbProtocol   = string.Empty;
		
		/*postgreSQL - driver connection*/
		private static Npgsql.NpgsqlConnection dbPostgresConnection;
		private static Npgsql.NpgsqlTransaction dbPostgresTransaction;
		//MS-SQL
		private static System.Data.SqlClient.SqlConnection databaseMSSQLConnection;
		private static System.Data.SqlClient.SqlTransaction msSQLTransaction;
		//MySQL
		private static MySql.Data.MySqlClient.MySqlConnection databaseMySQLConnection;
		private static MySql.Data.MySqlClient.MySqlTransaction mySQLTransaction;
		//Interbase/Firebird
		private static FirebirdSql.Data.FirebirdClient.FbConnection databaseFBSQLConnection;
		private static FirebirdSql.Data.FirebirdClient.FbTransaction fbSQLTransaction;
		//SQLite
		private static System.Data.SQLite.SQLiteConnection dbSqliteConnection;
		private static System.Data.SQLite.SQLiteTransaction sqliteTransaction;
		
		public const Boolean dbUseTransaction = true;
		public const Boolean dbNotUseTransaction = false;

		#region properties
		public static string  Name
		{
			get { return  dbName;      }
			set { dbName = value;      }
		}

		public static string  Host
		{
			get { return  dbHost; }
			set { dbHost = value; }
		}
		
		public static string  Port
		{
			get { return dbPort;      }
			set  { dbPort = value;      }
		}
		
		public static string  User
		{
			get { return dbUser;      }
			set  { dbUser = value;      }
		}

		public static string  Password
		{
			get { return dbPassword;  }
			set  { dbPassword = value;  }
		}

		public static Boolean IsConnected
		{
			//TODO: testar para cada banco se o State == ConnectionState.Open
			get { return dbConnected; }
		}

		public static string Protocol
		{
			get { return  dbProtocol; }
			set { dbProtocol = value; }
		}
		
		public static string  Info()
		{
			return dbName + " (" + dbHost + ")";
		}
		#endregion
		
		/*methods*/
		public static void  Connect()
		{
			string connectionstring;

			if (dbConnected)
			{
				result.Status = Status.SUCCESSFUL;
				result.ErrorMessage = "";
			}
			else
			{
				//Separa a porta do endereco ip
				if( dbHost.Contains(":"))
				{
					dbPort = dbHost.Split(':')[1];
					dbHost = dbHost.Split(':')[0];
				}
				
				//PostgreSQL
				if (dbProtocol == POSTGRESQL)
				{
					if(dbPort == string.Empty)
						dbPort = "5432";
					
					connectionstring = "Server=" + dbHost + ";Port=" + dbPort + ";User ID=" + dbUser +
						";Password=" + dbPassword + ";Database=" + dbName;
					if (dbPostgresConnection == null)
					{
						dbPostgresConnection = new Npgsql.NpgsqlConnection(connectionstring);
						try
						{
							dbPostgresConnection.Open();
						}
						catch (Exception e)
						{
							result.Status = Status.FAILED;
							result.ErrorMessage = e.Message;
							return;
						}
					}
					dbConnected = (dbPostgresConnection.State == ConnectionState.Open);
					result.Status = Status.SUCCESSFUL;
					result.ErrorMessage = "";
					return;
				}

				//Microsoft SQL-Server
				if (dbProtocol == "MSSQL")
				{
					connectionstring = "Server=" + dbHost + ";User ID=" + dbUser +
						";Password=" + dbPassword + ";Database=" + dbName +
						";Trusted_Connection=False";
					if (databaseMSSQLConnection == null)
					{
						databaseMSSQLConnection = new System.Data.SqlClient.SqlConnection(connectionstring);
						try
						{
							databaseMSSQLConnection.Open();
						}
						catch (Exception e)
						{
							result.Status = Status.FAILED;
							result.ErrorMessage = e.Message;
							return;
						}
					}
					dbConnected = (databaseMSSQLConnection.State == ConnectionState.Open);
					result.Status = Status.SUCCESSFUL;
					result.ErrorMessage = "";
					return;
				}
				//MySQL
				if (dbProtocol == MYSQL)
				{
					connectionstring = "Server=" + dbHost + ";User ID=" + dbUser +
						";Password=" + dbPassword + ";Database=" + dbName +
						";Allow Zero Datetime=true";
					if (databaseMySQLConnection == null)
					{
						databaseMySQLConnection = new MySql.Data.MySqlClient.MySqlConnection(connectionstring);
						try
						{
							databaseMySQLConnection.Open();
						}
						catch (Exception e)
						{
							result.Status = Status.FAILED;
							result.ErrorMessage = e.Message;
							return;
						}
					}
					dbConnected   = (databaseMySQLConnection.State == ConnectionState.Open);
					result.Status = Status.SUCCESSFUL;
					result.ErrorMessage = "";
					return;
				}
				//Firebird / Interbase
				if (dbProtocol == "Firebird")
				{
					connectionstring = "DataSource=" + dbHost + ";User=" + dbUser +
						";Password=" + dbPassword + ";Database=" + dbName;
					if (databaseFBSQLConnection == null)
					{
						databaseFBSQLConnection = new FirebirdSql.Data.FirebirdClient.FbConnection(connectionstring);
						try
						{
							databaseFBSQLConnection.Open();
						}
						catch (Exception e)
						{
							result.Status = Status.FAILED;
							result.ErrorMessage = e.Message;
							return;
						}
					}
					dbConnected = (databaseFBSQLConnection.State == ConnectionState.Open);
					result.Status = Status.SUCCESSFUL;
					result.ErrorMessage = "";
					return;
				}
				
				if (dbProtocol == SQLITE)
				{					
					connectionstring = "Data Source=" + dbName + ";Password="+Password;
					if (dbSqliteConnection == null)
					{
						dbSqliteConnection = new System.Data.SQLite.SQLiteConnection(connectionstring);
						try
						{
							dbSqliteConnection.Open();
						}
						catch (Exception e)
						{
							result.Status = Status.FAILED;
							result.ErrorMessage = e.Message;
							return;
						}
					}
					dbConnected = (dbSqliteConnection.State == ConnectionState.Open);
					result.Status = Status.SUCCESSFUL;
					result.ErrorMessage = "";
					return;
				}				
				
				result.Status = Status.FAILED;
				result.ErrorMessage = "Database protocol is not specified";
			}
		}

		public static void Disconnect()
		{
			if (dbConnected)
			{
				//PostgreSQL
				if (dbProtocol == POSTGRESQL)
				{
					dbPostgresConnection.Close();
				}
				//Microsoft SQL-Server
				if (dbProtocol == "MSSQL")
				{
					databaseMSSQLConnection.Close();
				}
				//MySQL
				if (dbProtocol == MYSQL)
				{
					databaseMySQLConnection.Close();
				}
				//Firebird/Interbase
				if (dbProtocol == "Firebird")
				{
					databaseFBSQLConnection.Close();
				}
				//Sqlite
				if (dbProtocol == SQLITE)
					dbSqliteConnection.Close();
				
				dbConnected = false;
			}
		}

		public static void BeginTransaction(System.Data.IsolationLevel dbIsolationLevel)
		{
			//PostgreSQL
			if ((dbProtocol == POSTGRESQL) && (dbPostgresTransaction == null))
			{
				dbPostgresTransaction = dbPostgresConnection.BeginTransaction(dbIsolationLevel);
			}
			//MSSQL
			if ((dbProtocol == "MSSQL") && (msSQLTransaction == null))
			{
				msSQLTransaction = databaseMSSQLConnection.BeginTransaction(dbIsolationLevel);
			}
			//MySQL
			if ((dbProtocol == MYSQL) && (mySQLTransaction == null))
			{
				mySQLTransaction = databaseMySQLConnection.BeginTransaction(dbIsolationLevel);
			}
			//Firebird/Interbase
			if ((dbProtocol == "Firebird") && (fbSQLTransaction == null))
			{
				fbSQLTransaction = databaseFBSQLConnection.BeginTransaction(dbIsolationLevel);
			}
			//Sqlite
			if ((dbProtocol == SQLITE) && (sqliteTransaction == null))
				sqliteTransaction = dbSqliteConnection.BeginTransaction(dbIsolationLevel);
		}

		public static void Commit()
		{
			//PostgreSQL
			if (dbProtocol == POSTGRESQL)
			{
				dbPostgresTransaction.Commit();
				dbPostgresTransaction = null;
			}
			//MSSQL
			if (dbProtocol == "MSSQL")
			{
				msSQLTransaction.Commit();
				msSQLTransaction = null;
			}
			//MySQL
			if (dbProtocol == MYSQL)
			{
				mySQLTransaction.Commit();
				mySQLTransaction = null;
			}
			//Firebird/Interbase
			if (dbProtocol == "Firebird")
			{
				fbSQLTransaction.Commit();
				fbSQLTransaction = null;
			}
			//Sqlite
			if (dbProtocol == SQLITE)
			{
				sqliteTransaction.Commit();
				sqliteTransaction = null;
			}
		}

		public static void RollBack()
		{
			//PostgreSQL
			if (dbProtocol == POSTGRESQL)
			{
				dbPostgresTransaction.Rollback();
				dbPostgresTransaction = null;
			}
			//MSSQL
			if (dbProtocol == "MSSQL")
			{
				msSQLTransaction.Rollback();
				msSQLTransaction = null;
			}
			//MySQL
			if (dbProtocol == MYSQL)
			{
				mySQLTransaction.Rollback();
				mySQLTransaction = null;
			}
			//Firebird/Interbase
			if (dbProtocol == "Firebird")
			{
				fbSQLTransaction.Rollback();
				fbSQLTransaction = null;
			}
			//Sqlite
			if (dbProtocol == SQLITE)
			{
				sqliteTransaction.Rollback();
				sqliteTransaction = null;
			}
		}

		public static object ExecuteScalar(string sql)
		{
			//PostgreSQL
			if (dbProtocol == POSTGRESQL)
			{
				Npgsql.NpgsqlCommand sqlCommand;
				Object myResultObject;
				
				try
				{
					sqlCommand = new Npgsql.NpgsqlCommand(sql, dbPostgresConnection, dbPostgresTransaction);
					myResultObject = sqlCommand.ExecuteScalar();
				}
				catch (Exception e)
				{
					result.Status = Status.FAILED;
					result.ErrorMessage = e.Message;
					
					LogErro.writeErrorSql(dbProtocol);
					LogErro.writeError(e.Message);
					return null;
				}
				
				sqlCommand.Dispose();
				result.Status = Status.SUCCESSFUL;
				result.ErrorMessage = "";
				return myResultObject;
			}
			//Microsoft SQL-Server
			if (dbProtocol == "MSSQL")
			{
				System.Data.SqlClient.SqlCommand sqlCommand;
				Object myResultObject;
				
				try
				{
					sqlCommand = new System.Data.SqlClient.SqlCommand(sql, databaseMSSQLConnection, msSQLTransaction);
					myResultObject = sqlCommand.ExecuteScalar();
				}
				catch (Exception e)
				{
					result.Status = Status.FAILED;
					result.ErrorMessage = e.Message;
					
					LogErro.writeErrorSql(dbProtocol);
					LogErro.writeError(e.Message);
					return null;
				}
				
				sqlCommand.Dispose();
				result.Status = Status.SUCCESSFUL;
				result.ErrorMessage = "";
				return myResultObject;
			}
			//MySQL
			if (dbProtocol == MYSQL)
			{
				MySql.Data.MySqlClient.MySqlCommand sqlCommand;
				Object myResultObject;
				
				try
				{
					sqlCommand = new MySql.Data.MySqlClient.MySqlCommand(sql, databaseMySQLConnection, mySQLTransaction);
					myResultObject = sqlCommand.ExecuteScalar();
				}
				catch (Exception e)
				{
					result.Status = Status.FAILED;
					result.ErrorMessage = e.Message;
					
					LogErro.writeErrorSql(dbProtocol);
					LogErro.writeError(e.Message);
					return null;
				}
				
				sqlCommand.Dispose();
				result.Status = Status.SUCCESSFUL;
				result.ErrorMessage = "";
				return myResultObject;
			}
			//Firebird/Interbase
			if (dbProtocol == "Firebird")
			{
				FirebirdSql.Data.FirebirdClient.FbCommand sqlCommand;
				Object myResultObject;
				
				try
				{
					sqlCommand = new FirebirdSql.Data.FirebirdClient.FbCommand(sql, databaseFBSQLConnection, fbSQLTransaction);
					myResultObject = sqlCommand.ExecuteScalar();
				}
				catch (Exception e)
				{
					result.Status = Status.FAILED;
					result.ErrorMessage = e.Message;
					
					LogErro.writeErrorSql(dbProtocol);
					LogErro.writeError(e.Message);
					return null;
				}
				
				sqlCommand.Dispose();
				result.Status = Status.SUCCESSFUL;
				result.ErrorMessage = "";
				return myResultObject;
			}
			
			//Sqlite
			if (dbProtocol == SQLITE)
			{
				System.Data.SQLite.SQLiteCommand sqlCommand;
				Object myResultObject;
				
				try
				{
					sqlCommand = new System.Data.SQLite.SQLiteCommand(sql, dbSqliteConnection, sqliteTransaction);
					myResultObject = sqlCommand.ExecuteScalar();
				}
				catch (Exception e)
				{
					result.Status = Status.FAILED;
					result.ErrorMessage = e.Message;
					
					LogErro.writeErrorSql(dbProtocol);
					LogErro.writeError(e.Message);
					return null;
				}
				
				sqlCommand.Dispose();
				result.Status = Status.SUCCESSFUL;
				result.ErrorMessage = "";
				return myResultObject;				
			}
			result.Status = Status.FAILED;
			result.ErrorMessage = "Database protocol is not specified";
			return null;
		}
		
		public static bool  ExecuteCommand(string sql, Boolean useTransaction)
		{
			//PostgreSQL
			if (dbProtocol == POSTGRESQL)
			{
				Npgsql.NpgsqlCommand sqlCommand;
				
				if (useTransaction)
				{
					BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
				}
				sqlCommand = new Npgsql.NpgsqlCommand(sql, dbPostgresConnection, dbPostgresTransaction);
				try
				{
					sqlCommand.ExecuteNonQuery();
					if (useTransaction)
					{
						Commit();
					}
					result.Status = Status.SUCCESSFUL;
					result.ErrorMessage = "";
					return true;
				}
				catch (Exception e)
				{
					if (useTransaction)
					{
						RollBack();
					}
					result.Status = Status.FAILED;
					result.ErrorMessage = e.Message;
					
					LogErro.writeErrorSql(sql);
					LogErro.writeError(e.Message);
					return false;
				}
			}
			//Microsoft SQL-Server
			if (dbProtocol == "MSSQL")
			{
				System.Data.SqlClient.SqlCommand sqlCommand;
				
				if (useTransaction)
				{
					BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
				}
				sqlCommand = new System.Data.SqlClient.SqlCommand(sql, databaseMSSQLConnection, msSQLTransaction);
				try
				{
					sqlCommand.ExecuteNonQuery();
					if (useTransaction)
					{
						Commit();
					}
					result.Status = Status.SUCCESSFUL;
					result.ErrorMessage = "";
					return true;
				}
				catch (Exception e)
				{
					if (useTransaction)
					{
						RollBack();
					}
					result.Status = Status.FAILED;
					result.ErrorMessage = e.Message;
					
					LogErro.writeErrorSql(sql);
					LogErro.writeError(e.Message);
					return false;
				}
			}
			//MySQL
			if (dbProtocol == MYSQL)
			{
				MySql.Data.MySqlClient.MySqlCommand sqlCommand;
				
				if (useTransaction)
				{
					BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
				}
				sqlCommand = new MySql.Data.MySqlClient.MySqlCommand(sql, databaseMySQLConnection, mySQLTransaction);
				try
				{
					sqlCommand.ExecuteNonQuery();
					if (useTransaction)
					{
						Commit();
					}
					result.Status = Status.SUCCESSFUL;
					result.ErrorMessage = string.Empty;
					return true;
				}
				catch (Exception e)
				{
					if (useTransaction)
					{
						RollBack();
					}
					result.Status = Status.FAILED;
					result.ErrorMessage = e.Message;
					
					LogErro.writeErrorSql(sql);
					LogErro.writeError(e.Message);
					return false;
				}
			}
			//Firebird/Interbase
			if (dbProtocol == "Firebird")
			{
				FirebirdSql.Data.FirebirdClient.FbCommand sqlCommand;
				
				if (useTransaction)
				{
					BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
				}
				sqlCommand = new FirebirdSql.Data.FirebirdClient.FbCommand(sql, databaseFBSQLConnection, fbSQLTransaction);
				try
				{
					sqlCommand.ExecuteNonQuery();
					if (useTransaction)
					{
						Commit();
					}
					result.Status = Status.SUCCESSFUL;
					result.ErrorMessage = string.Empty;
					return true;
				}
				catch (Exception e)
				{
					if (useTransaction)
					{
						RollBack();
					}
					result.Status = Status.FAILED;
					result.ErrorMessage = e.Message;
					
					LogErro.writeErrorSql(sql);
					LogErro.writeError(e.Message);
					return false;
				}
			}
			
			if (dbProtocol == SQLITE)
			{
				System.Data.SQLite.SQLiteCommand sqlCommand;
				
				if (useTransaction)
					BeginTransaction(IsolationLevel.ReadCommitted);
				try
				{
					sqlCommand = new System.Data.SQLite.SQLiteCommand(sql, dbSqliteConnection, sqliteTransaction);
					sqlCommand.ExecuteNonQuery();
					if (useTransaction)
						Commit();
					
					result.Status = Status.SUCCESSFUL;
					result.ErrorMessage = string.Empty;
					return true;
				}
				catch (Exception e)
				{
					if (useTransaction)
					{
						RollBack();
					}
					result.Status = Status.FAILED;
					result.ErrorMessage = e.Message;
					
					LogErro.writeErrorSql(sql);
					LogErro.writeError(e.Message);
					return false;
				}				
			}
			result.Status = Status.FAILED;
			result.ErrorMessage = "Database protocol is not specified";
			return false;
		}

		public static DataSet ExecuteQuery(string sql)
		{
			//PostgreSQL
			if (dbProtocol == POSTGRESQL)
			{
				Npgsql.NpgsqlCommand sqlCommand;
				Npgsql.NpgsqlDataReader sqlDataReader;
				System.Data.DataSet sqlDataSet = new System.Data.DataSet();

				try
				{
					sqlCommand = new Npgsql.NpgsqlCommand(sql, dbPostgresConnection, dbPostgresTransaction);
					sqlDataReader = sqlCommand.ExecuteReader();
					sqlDataSet.Load(sqlDataReader, System.Data.LoadOption.OverwriteChanges, "table");
				}
				catch (Exception e)
				{
					result.Status = Status.FAILED;
					result.ErrorMessage = e.Message;
					
					LogErro.writeErrorSql(dbProtocol);
					LogErro.writeError(e.Message);
					return null;
				}

				sqlDataReader.Close();
				sqlDataReader.Dispose();
				sqlCommand.Dispose();
				result.Status = Status.SUCCESSFUL;
				result.ErrorMessage = "";
				return sqlDataSet;
			}
			//Microsoft SQL-Server
			if (dbProtocol == "MSSQL")
			{
				System.Data.SqlClient.SqlCommand sqlCommand;
				System.Data.SqlClient.SqlDataReader sqlDataReader;
				System.Data.DataSet sqlDataSet = new System.Data.DataSet();

				try
				{
					sqlCommand = new System.Data.SqlClient.SqlCommand(sql, databaseMSSQLConnection, msSQLTransaction);
					sqlDataReader = sqlCommand.ExecuteReader();
					sqlDataSet.Load(sqlDataReader, System.Data.LoadOption.OverwriteChanges, "table");
				}
				catch (Exception e)
				{
					result.Status = Status.FAILED;
					result.ErrorMessage = e.Message;
					return null;
				}

				sqlDataReader.Close();
				sqlDataReader.Dispose();
				sqlCommand.Dispose();
				result.Status = Status.SUCCESSFUL;
				result.ErrorMessage = "";
				return sqlDataSet;
			}
			//MySQL
			if (dbProtocol == MYSQL)
			{
				MySql.Data.MySqlClient.MySqlCommand sqlCommand;
				MySql.Data.MySqlClient.MySqlDataReader sqlDataReader;
				System.Data.DataSet sqlDataSet = new System.Data.DataSet();

				try
				{
					sqlCommand = new MySql.Data.MySqlClient.MySqlCommand(sql, databaseMySQLConnection, mySQLTransaction);
					sqlDataReader = sqlCommand.ExecuteReader();
					sqlDataSet.Load(sqlDataReader, System.Data.LoadOption.OverwriteChanges, "table");
				}
				catch (Exception e)
				{
					result.Status = Status.FAILED;
					result.ErrorMessage = e.Message;
					return null;
				}

				sqlDataReader.Close();
				sqlCommand.Dispose();
				result.Status = Status.SUCCESSFUL;
				result.ErrorMessage = "";
				return sqlDataSet;
			}
			//Firebird/Interbase
			if (dbProtocol == "Firebird")
			{
				FirebirdSql.Data.FirebirdClient.FbCommand sqlCommand;
				FirebirdSql.Data.FirebirdClient.FbDataReader sqlDataReader;
				System.Data.DataSet sqlDataSet = new System.Data.DataSet();

				try
				{
					sqlCommand = new FirebirdSql.Data.FirebirdClient.FbCommand(sql, databaseFBSQLConnection, fbSQLTransaction);
					sqlDataReader = sqlCommand.ExecuteReader();
					sqlDataSet.Load(sqlDataReader, System.Data.LoadOption.OverwriteChanges, "table");
				}
				catch (Exception e)
				{
					result.Status = Status.FAILED;
					result.ErrorMessage = e.Message;
					return null;
				}

				sqlDataReader.Close();
				sqlCommand.Dispose();
				result.Status = Status.SUCCESSFUL;
				result.ErrorMessage = "";
				return sqlDataSet;
			}
			
			//Sqlite
			if (dbProtocol == SQLITE)
			{
				System.Data.SQLite.SQLiteCommand sqlCommand;
				System.Data.SQLite.SQLiteDataReader sqlDataReader;
				System.Data.DataSet sqlDataSet = new DataSet();
				
				try
				{
					sqlCommand = new System.Data.SQLite.SQLiteCommand(sql, dbSqliteConnection,sqliteTransaction);
					sqlDataReader = sqlCommand.ExecuteReader();
					sqlDataSet.Load(sqlDataReader, System.Data.LoadOption.OverwriteChanges, "table");
				}
				catch(Exception e)
				{
					result.Status = Status.FAILED;
					result.ErrorMessage = e.Message;
					return null;
				}
				
				sqlDataReader.Close();
				sqlCommand.Dispose();
				result.ErrorMessage = string.Empty;
				return sqlDataSet;
			}
			result.Status = Status.FAILED;
			result.ErrorMessage = "Database protocol is not specified";
			return null;
		}

		public static Int32 GetSerial(string serialName)
		{ 
			//PostgreSQL
			if (dbProtocol == POSTGRESQL)
			{
			    return System.Convert.ToInt32(ExecuteScalar("SELECT nextval('" + serialName + "') AS codigo_serial"));
			}            
			//Firebird/Interbase
			if (dbProtocol == "Firebird")
			{
			    return System.Convert.ToInt32(ExecuteScalar("SELECT GEN_ID('" + serialName + "',1) AS codigo_serial FROM RDB$DATABASE"));
			}
			return 0;
		}		
		
		public static Npgsql.NpgsqlConnection getConnection()
		{
			return dbPostgresConnection;
		}
		
		public static System.Data.SQLite.SQLiteConnection getSqliteConnection()
		{
			return dbSqliteConnection;
		}
		
		public static void createDatabase(bool setPassword)
		{
			System.Data.SQLite.SQLiteConnection.CreateFile(dbName);
			
			if (setPassword)
			{
				System.Data.SQLite.SQLiteConnection tmp = new System.Data.SQLite.SQLiteConnection("Data Source=" + dbName);
				tmp.SetPassword(dbPassword);
				tmp.Dispose();
			}
		}
	}
}
